<!--
Date: 2025-08-13
Copyright (c) 2025 CAX Conversion Project
-->
<template>
  <el-dialog v-model="props.addVisible" :title="modalType === 'add' ? '新增' : '修改'" width="500" destroy-on-close :before-close="handleVisible">
    <el-form size="small" :rules="rules" :model="formData" label-position="right" label-width="80px" ref="formRef">
      <el-form-item label="算子名" prop="operatorName">
        <el-input placeholder="请输入" :disabled="modalType === 'modify'" v-model="formData.operatorName" />
      </el-form-item>
      <el-form-item label="算子实现" prop="algorithmRealize">
        <el-input placeholder="请输入" v-model="formData.algorithmRealize" />
      </el-form-item>
      <el-form-item label="计算对象" prop="caculate">
        <el-cascader collapse-tags="true" max-collapse-tags="1" v-model="formData.caculate" :options="calculateObject" :props="caculateProps" clearable placeholder="请选择" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-cascader collapse-tags="true" max-collapse-tags="1" v-model="formData.type" :options="props.operatorType" :props="typeProps" clearable placeholder="请选择" />
      </el-form-item>
      <el-form-item label="备注" prop="operatorDescription">
        <el-input v-model="formData.operatorDescription" :rows="3" type="textarea" placeholder="请输入" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="small" @click="handleVisible">取消</el-button>
        <el-button size="small" type="primary" @click="submitAdd"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
  
<script name="operator" setup>
import { calculateObject } from '../algorithmLibrary.js';
import { addOperator, updateOperator } from '@/api/algorithmLibrary';
const { proxy } = getCurrentInstance();
const caculateProps = { multiple: true };
const typeProps = { multiple: false };
const props = defineProps(['addVisible', 'rowData','operatorType']);
const emit = defineEmits(['close']);
const formRef = ref();
// 表单校验规则
const rules = ref({
  operatorName: [{ required: true, message: '请输入算子名', trigger: 'blur' }],
  algorithmRealize: [{ required: true, message: '请输入算子实现', trigger: 'blur' }],
  type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
  caculate: [{ required: true, message: '请选择计算对象', trigger: 'blur' }]
});
const formData = ref({
  operatorName: props?.rowData?.operatorName,
  algorithmRealize: props?.rowData?.algorithmRealize,
  type: '',
  caculate: props?.rowData?.coculate,
  operatorDescription: props?.rowData?.operatorDescription
});
const modalType = props?.rowData ? 'modify' : 'add';
// 修改数据处理
if (modalType === 'modify') {
  const data = props?.rowData;
  if (data) {
    formData.value.type = [data.applicationType, data.materialType, data.algorithmType];
    const caculate = data.calculateObjects || [];
    let caculateArr = [];
    caculate.map((el) => {
      const arr = el.objectName ? [el.objectType, el.objectName] : [el.objectType];
      caculateArr.push(arr);
    });
    formData.value.caculate = caculateArr;
    formData.value._id = data._id;
  }
}
// 通知父组件
const handleVisible = (modalType) => {
  emit('close', { type: 'add', data: { type: modalType === 'refesh' ? 'refesh' : '' } });
};
// 提交数据
const submitAdd = () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      return;
    } else {
      // 处理数据
      formData.value.applicationType = formData.value.type[0];
      formData.value.materialType = formData.value.type[1];
      formData.value.algorithmType = formData.value.type[2];
      const caculate = formData.value.caculate;
      let caculateObj = [];
      caculate.map((el) => {
        if (el.length > 1) {
          caculateObj.push({ objectType: el[0], objectName: el[1] });
        } else {
          caculateObj.push({ objectType: el[0], objectName: '' });
        }
      });
      formData.value.calculateObjects = caculateObj;
      delete formData.value.caculate;
      delete formData.value.type;
      if (modalType === 'modify') {
        updateOperator(formData.value).then((res) => {
          if (res?.code === 200) {
            handleVisible('refesh');
            proxy?.$modal.msgSuccess('修改成功');
          }
        });
      } else {
        addOperator(formData.value).then((res) => {
          if (res?.code === 200) {
            handleVisible('refesh');
            proxy?.$modal.msgSuccess('新增成功');
          }
        });
      }
    }
  });
};
</script>
  <style scoped>
:deep(.el-cascader) {
  width: 100%;
}
</style>